﻿using SQLite;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;

namespace SQLiteCoreDB.Context
{
    public class SQLiteContext
    {
      
        //public static readonly string SettingsDbPath = Path.Combine(
        //    Environment.GetFolderPath(
        //        Environment.SpecialFolder.LocalApplicationData), Configs.DbName); 
        
        public static readonly string DbPath = Path.Combine(
           Directory.GetCurrentDirectory(), Configs.DbName);
        public SQLiteContext()
        {

        }
        private SQLiteAsyncConnection _connection;
        public SQLiteAsyncConnection Connection => _connection ??= GetConnection();


        public async Task InitializeDbAsync<T>() where T : class, new()
        {
            await Connection.CreateTableAsync<T>();
        }
        public bool IsDBExists()
        {
            //检查数据库是否存在
            return File.Exists(DbPath);

        }

        public async  Task<bool> IstableExists<T>() where T : class, new()
        {
            //检查表是否存在
            return await Connection.Table<T>().CountAsync() > 0;
 

        }

        //泛型table
        public async Task<List<T>> GetListAsync<T>(Expression<Func<T, bool>> predicate) where T : class, new()
        {
            try
            {
                // 直接执行异步查询并返回结果列表
                return await Connection.Table<T>().Where(predicate).ToListAsync();
            }
            catch (Exception ex)
            {
                // 异常处理
                throw ex;
            }
        }
        public async Task<List<T>> GetListAsync<T>() where T : class, new()
        {
            try
            {
                // 直接执行异步查询并返回结果列表
                return await Connection.Table<T>().ToListAsync();
            }
            catch (Exception ex)
            {
                // 异常处理
                throw ex;
            }
        }
        public async Task<T> GetOneAsync<T>(Expression<Func<T, bool>> predicate) where T : class, new()
        {
            try
            {
                return await Connection.Table<T>().Where(predicate).FirstOrDefaultAsync();
            }
            catch (Exception ex)
            {

                throw ex;
            }

        }
        public async Task<int> AddAsync<T>(T entity) where T : class
        {
            if (entity is null)
            {
                throw new ArgumentNullException(nameof(entity), "请勿传递空数据.");
            }

            try
            {
                //插入数据
                return await Connection.InsertAsync(entity);
            }
            catch (Exception ex)
            {

                throw ex;
            }
        }

        public async Task<int> UpdateAsync<T>(T entity) where T : class, new()
        {
            if (entity is null)
            {
                throw new ArgumentNullException(nameof(entity), "请勿传递空数据.");
            }

            try
            {
                // 执行异步更新操作
                return await Connection.UpdateAsync(entity);
            }
            catch (Exception ex)
            {

                throw;
            }
        }
        public async Task<int> DeleteAsync<T>(T entity) where T : class
        {
            if (entity is null)
            {
                throw new ArgumentNullException(nameof(entity), "请勿传递空数据.");
            }

            try
            {
                //删除数据
                return await Connection.DeleteAsync(entity);
            }
            catch (Exception ex)
            {

                throw ex;
            }
        }



        private SQLiteAsyncConnection GetConnection()
        {
            return new SQLiteAsyncConnection(DbPath);
        }
    }


}
